import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import NCBackBtn from "src/hrpub/common/components/hr-back";
import {chectFormAction} from "../../../../../public/functions/checkFormAction";

const {NCAffix} = base;

class AgreementDetail extends Component {
    constructor(props) {
        super(props);
        this.pageQueryClick = this.pageQueryClick.bind(this);
        this.goBack = this.goBack.bind(this);
        this.onAfterAddFormEvent = this.onAfterAddFormEvent.bind(this);
        this.beforeEventHandle = this.beforeEventHandle.bind(this);
    }

    componentDidMount() {
        if (this.props.data) {
            /*this.props.cardPagination.getNextCardPaginationId({
                id: this.props.data.rows[0].values.pk_psndoc_sub.value,
                status: 1
            });*/
            this.props.cardPagination.setCardPaginationId({
                id: this.props.data.rows[0].values.pk_agreement.value,
                status: 1
            });
        }
    }

    pageQueryClick(props, key) {
        const record = props.table.getAllTableData('agreementList').rows.find(row => row.values.pk_agreement.value === key);
        this.props.view('', record.values)
    }

    goBack() {
        const {pageInfo, updateState, updateButton, getAgreeTableData, isQueryTemplate} = this.props;
        updateState({
            agreementPageInfo: {...pageInfo, pageIndex: 1},
            agreementPage: 'main'
        }, () => {
            updateButton();
            getAgreeTableData(isQueryTemplate);
        })
    }

    beforeEventHandle(props, moduleId, key, item, index, value, record) {
        // 过滤协议模板信息
        let {areaCode, orgVal} = this.props;
        if (key === 'pk_conttext') {
            let meta = props.meta.getMeta();
            let editArea = areaCode;
            // const formrelation = meta.formrelation && meta.formrelation[areaCode];
            // if (formrelation) {
            //     editArea = formrelation[formrelation.length - 1];
            // }
            if(value.belongTo) {
                editArea = value.belongTo
            }
            
            const agreementtype = props.form.getAllFormValue(areaCode).rows[0].values.agreementtype.value;
            meta[editArea].items.find(item => item.attrcode === key).queryCondition = function () {
                return {
                    "pk_org": orgVal.refpk,
                    "GridRefActionExt": "nccloud.web.hrcm.make.sqlbuilder.ContModelGridSqlBuilder",
                    "agreementType": agreementtype
                }
            };
            props.meta.setMeta(meta);
        }
        return true;
    }

    onAfterAddFormEvent(props, moduleId, key, value) {
        //handleAfter.call(this, moduleId, key, value);
        const {areaCode, appCode} = this.props;
        let editArea = areaCode;
        const meta = this.props.meta.getMeta();
        const formrelation = meta.formrelation && meta.formrelation[areaCode];
        if (formrelation) {
            editArea = formrelation[formrelation.length - 1];
        }
        chectFormAction(key, areaCode, props, [editArea], appCode)
    }

    render() {
        const {form, cardPagination, page, areaCode} = this.props;
        const {createCardPagination} = cardPagination;
        return (
            <div>
                {/*<NCAffix>*/}
                <div className="header" style={{display: page === 'view' ? '' : 'none'}}>
                    <NCBackBtn onClick={this.goBack}/>
                    {createCardPagination({handlePageInfoChange: this.pageQueryClick})}
                </div>
                {/* </NCAffix>*/}
                {areaCode ? form.createForm(areaCode, {
                    onBeforeEvent: this.beforeEventHandle,
                    onAfterEvent: this.onAfterAddFormEvent
                }) : null}
            </div>
        );
    }
}

export default AgreementDetail;